package com.pacyki.gcards;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.helpfuctions.gcards.ContactsScreen;
import com.helpfuctions.gcards.ScreenShot;
import com.pacyki.adapters.ImagesAdapter;

public class GreetingCardsActivity extends Activity implements OnClickListener, OnTouchListener{
	final int INVISIBLE_VIEW = 1000;
	final int VISIBLE_VIEW = 0;
	
	private String[] mImageCategories = {"New Year", "8th of March", "S't Valentine's Day", "Birthday"};	
	private String[] mImages = {"blue.png", "hearts_green.png", "ic_launcher.png",
		"march.png", "rabbit.png", "red.png"};
	
	private HorizontalScrollView mMainAppScroll;
	private LinearLayout mMainAppConatiner;
	
	private RelativeLayout mPostCardContainer; //container with bars, action button and image
	private Button mProceedButton;	
	private HorizontalScrollView mUpperToolbarScroll;
	private LinearLayout mUpperToolbarContainer;
	private ScrollView mLeftToolbarScroll;
	private LinearLayout mLeftToolbarContainer;
	private ImageView mPostCard;
	
	private Context mContext;
	private LayoutInflater mInflater;
	private EditText mPostCardText;
	private ScreenShot mScreenShot;
	
	private boolean mScrollDirection;
	private float mTouchDownX;
	private float mTouchUpX;
	
	private Display mDisplay;
	private View contactsScreen;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_screen);
        mDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        initView();
        
        mProceedButton.setOnClickListener(this);
        mMainAppScroll.setOnTouchListener(this);
        
        ImagesAdapter imagesAdapter = new ImagesAdapter(this, mImages);
        
        for (int elementIdx = 0; elementIdx < imagesAdapter.getCount(); elementIdx++) {
        	View currentView = imagesAdapter.getView(elementIdx, null, null);
        	currentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        	mLeftToolbarContainer.addView(currentView);
        }
        
        for (int i = 0; i < 100; i++) {
			mUpperToolbarContainer.addView(new Button(this));
		}
        
        
        mScreenShot = new ScreenShot(mPostCard);
    }
    
    private void initView() {
    	mProceedButton = (Button) findViewById(R.id.actionButton);
    	mUpperToolbarScroll = (HorizontalScrollView) findViewById(R.id.upperToolbar);
    	mLeftToolbarScroll = (ScrollView) findViewById(R.id.leftToolbar);
    	mMainAppScroll = (HorizontalScrollView) findViewById(R.id.screenScroll);
    	mMainAppScroll.setFadingEdgeLength(0);
    	mUpperToolbarContainer = (LinearLayout) findViewById(R.id.upperToolbarContainer);
    	mLeftToolbarContainer = (LinearLayout) findViewById(R.id.leftToolbarContainer);
    	mMainAppConatiner = (LinearLayout) findViewById(R.id.screenContainer);
    	mPostCard = (ImageView) findViewById(R.id.postCardImage);
    	mPostCardContainer = (RelativeLayout) findViewById(R.id.postCardContainer);
    	
    	mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    	
    	View emptyScreen = mInflater.inflate(R.layout.empty_screen, null);
    	mMainAppConatiner.addView(emptyScreen, mDisplay.getWidth(), mDisplay.getHeight());
        View textsScreen = mInflater.inflate(R.layout.text_edit, null);
        mMainAppConatiner.addView(textsScreen, mDisplay.getWidth(), mDisplay.getHeight());
        contactsScreen = mInflater.inflate(R.layout.contacts_screen, null);
        mMainAppConatiner.addView(contactsScreen, mDisplay.getWidth(), mDisplay.getHeight());
        mContext = getApplicationContext();
        fillContactScreen();
    }
    
	@Override
	public void onClick(View obj) {
		switch (obj.getId()) {
		case R.id.actionButton:
			break;
		}
	}

	@Override
	public boolean onTouch(View scroll, MotionEvent event) {
		switch (scroll.getId()) {
		case R.id.screenScroll:
			Log.d("dashboard","" + event.getAction());
			HorizontalScrollView currentScroll = (HorizontalScrollView) scroll;
			currentScroll.setSmoothScrollingEnabled(true);
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					//Log.d("dashboard", "ActionDown");
					mTouchDownX = event.getX();
					break;
				case MotionEvent.ACTION_MOVE:
					//Log.d("dashboard", "ActionMove");
					break;
				case MotionEvent.ACTION_UP:
					//Log.d("dashboard", "ActionUp");
					mTouchUpX = event.getX();
					mScrollDirection = (mTouchUpX < mTouchDownX);
					Log.d("dashboard", "X_DOWN " + mTouchDownX + " X_UP " + mTouchUpX + 
							" SCROLL_DIR " + mScrollDirection + " SCROLL " + scroll.getScrollX() +
							" SCREEN " + mDisplay.getWidth());
					float position = scroll.getScrollX();
					float displayWidth = mDisplay.getWidth();
					
					if (mScrollDirection) {
						//>>
						if (position > displayWidth * 5 / 4) {
							currentScroll.smoothScrollTo((int) displayWidth * 2, 0);
							mPostCardContainer.setVisibility(INVISIBLE_VIEW);
							mPostCard.setVisibility(INVISIBLE_VIEW);
							//mPostCard.offsetLeftAndRight(-100);
							return true;
						}
						
						if ((position > displayWidth) && (position <= displayWidth * 5 / 4)) {
							currentScroll.smoothScrollTo((int) displayWidth, 0);
							return true;
						}
						
						if (position > displayWidth / 4) {
							currentScroll.smoothScrollTo((int) displayWidth, 0);
							return true;
						} 
						
						if (position <= displayWidth / 4) {
							currentScroll.smoothScrollTo(0, 0);
							return true;
						}
					}else {
						//<<
						if (position >= displayWidth * 7 / 4) {
							currentScroll.smoothScrollTo((int) displayWidth * 2, 0);
							return true;
						}
								
						if ((position > displayWidth * 3 / 4) && (position < displayWidth * 7 / 4)) {
							currentScroll.smoothScrollTo((int) displayWidth, 0);
							mPostCardContainer.setVisibility(VISIBLE_VIEW);
							mPostCard.setVisibility(VISIBLE_VIEW);
							return true;
						}
						
						if (position <= displayWidth * 3 / 4) {
							currentScroll.smoothScrollTo(0, 0);
							return true;
						}
					}
					
					break;
					
				default:
					break;
				}
			break;

		default:
			break;
		}
		return false;
	}
	
	private void fillContactScreen(){
		ContentResolver contentResolver = getContentResolver();
		ContactsScreen fillContactsScreen = new ContactsScreen(contactsScreen, mContext, contentResolver);
	}
}